/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Mensajeria;

import com.sun.mail.smtp.SMTPTransport;
import java.security.Security;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

/**
 *
 * @author USER
 */
public class EnviarCorreo {

    public int Enviar(String Para, String CCopia, String Asunto, String Cuerpo) throws Exception{

       return  ObjCorreo(Para, CCopia, Asunto, Cuerpo);

    }

    private int ObjCorreo(String Para, 
                           String CCopia, 
                           String Asunto, 
                           String Cuerpo) throws Exception{
        
        int Enviado = 0;

        final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";


        Properties oProperties = System.getProperties();
        oProperties.setProperty("mail.smtps.host", "smtp.gmail.com");
        oProperties.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
        oProperties.setProperty("mail.smtp.socketFactory.fallback", "false");
        oProperties.setProperty("mail.smtp.port", "587");
        oProperties.setProperty("mail.smtp.socketFactory.port", "587");
        oProperties.setProperty("mail.smtps.auth", "true");
        oProperties.put("mail.smtps.quitwait", "false");

        Session oSession = Session.getInstance(oProperties, null);
        final MimeMessage oMimeMessage = new MimeMessage(oSession);

        CuentaCorreo oCuentaCorreo = new CuentaCorreo();

        oMimeMessage.setFrom(new InternetAddress(oCuentaCorreo.getUsername() + "@gmail.com"));
        oMimeMessage.setRecipients(Message.RecipientType.TO, InternetAddress.parse(Para, false));
        oMimeMessage.setSubject(Asunto);

        if (CCopia.length() > 0) {
            oMimeMessage.setRecipients(Message.RecipientType.CC, InternetAddress.parse(CCopia, false));
        }

        MimeBodyPart oMimeBodyPart = new MimeBodyPart();
        //mbp1.setText("<table border=1><tr><td>hola</td></tr></table>");

        oMimeBodyPart.setContent(Cuerpo, "text/html");

        // create the Multipart and add its parts to it
        Multipart oMultipart = new MimeMultipart();
        oMultipart.addBodyPart(oMimeBodyPart);
        //mp.addBodyPart(mbp2);

        // add the Multipart to the message
        oMimeMessage.setContent(oMultipart);

        // set the Date: header
        oMimeMessage.setSentDate(new Date());

        SMTPTransport oSMTPTransport = (SMTPTransport) oSession.getTransport("smtps");

        oSMTPTransport.connect("smtp.gmail.com", oCuentaCorreo.getUsername(), oCuentaCorreo.getPassword());
        oSMTPTransport.sendMessage(oMimeMessage, oMimeMessage.getAllRecipients());
        oSMTPTransport.close();
        
        Enviado = 1;
        
        return Enviado;

    }
}
